﻿using UnityEngine;
using System.Collections;

public class Inventory : MonoBehaviour, Hittable {


	GUImanager myGUImanager;


	float health;
	float sheild;
	public bool hasSheild;

	int bulletAmmo;
	public bool hasMachineGun;

	public bool hasCharge;
	bool canCharge;
	float chargeTimer;
	float chargeTimerMax;

	int rockets;
	public bool hasRockets;

	public int GetBulletAmmo(){return bulletAmmo;}
	public int GetRockets(){return rockets;}
	public bool CanCharge() {return canCharge;}



	void Start()
	{
		health = 100.0f;
		sheild = 100.0f;
		myGUImanager = GetComponent<GUImanager> ();
		bulletAmmo = 0;
		canCharge = true;
		chargeTimer = 0.0f;
		chargeTimerMax = 1.5f;
		rockets = 0;

		myGUImanager.CreateHealthbar ();
		if (hasSheild)
			myGUImanager.CreateSheildbar ();
		if(hasCharge)
			myGUImanager.CreateChargebar();
		if(hasMachineGun)
			myGUImanager.CreateAmmoDisplay();
		if(hasRockets)
			myGUImanager.CreateRocketDisplay();
	}

	public void GetPickup(GameObject pickupType)
	{
		if(pickupType.tag == "CoinPickup")
		{
			rockets ++;
			SetRockets(rockets);

		}
		else if(pickupType.tag == "AmmoPickup")
		{
			bulletAmmo += 20;
			SetAmmo(bulletAmmo);
		}
		else if(pickupType.tag == "HealthPickup")
		{
			AdjustHealth(10.0f);
		}
		else if(pickupType.tag == "SheildPickup")
		{
			AdjustSheild(10.0f);
		}
		else
			Debug.Log ("ERROR: pickup is not recognised");
	}

	public void Hit(float damage)
	{
		if(hasSheild && sheild > 0)
			AdjustSheild(-damage);
		else
			AdjustHealth (-2 * damage);
	}

	// Update is called once per frame
	void Update ()
	{
		if(!CanCharge())
			RunChargetimer();
	}

	//this function will run a timer then reset the ability to charge once capable
	void RunChargetimer()
	{
		chargeTimer += Time.deltaTime;


		if(chargeTimer >= chargeTimerMax)
		{
			SetCharge(chargeTimerMax);
			chargeTimer = 0.0f;
			canCharge = true;
		}
		else
			SetCharge(chargeTimer);
	}
	//retruns if possible to charge and sets timer
	public bool UseCharge()
	{
		if(canCharge)
		{
			canCharge = false;
			return true;
		}
		else
			return false;
	}

	//
	//This section is To adjust the gui elements based on the changed inventory
	//
	#region GUI update
	//HEALTH
	void SetHealth(float h)
	{
		health += h;
		if (health < 0)
			health = 0;
		else if (health > 100.0f)
			health = 100.0f;
		myGUImanager.healthBarScript.SetBarPercent (health);
	}
	void AdjustHealth(float h)
	{
		health += h;
		if (health < 0)
			health = 0;
		else if (health > 100.0f)
			health = 100.0f;

		myGUImanager.healthBarScript.SetBarPercent (health);
	}
	
	//SHEILD
	void SetSheild(float s)
	{
		sheild += s;
		if (sheild < 0)
			sheild = 0;
		else if (sheild > 100.0f)
			sheild = 100.0f;
		sheild = s;
		myGUImanager.sheildBarScript.SetBarPercent (sheild);
	}
	void AdjustSheild(float s)
	{
		sheild += s;
		if (sheild < 0)
			sheild = 0;
		else if (sheild > 100.0f)
			sheild = 100.0f;
		myGUImanager.sheildBarScript.SetBarPercent (sheild);
	}
	
	//CHARGE
	void SetCharge (float c)
	{
		if(c < 0 || c > chargeTimerMax)
		{
			Debug.Log ("ERROR: Charge must be be a fraction of the charge timer");
			Mathf.Clamp(c, 0.0f , chargeTimerMax);
		}
		myGUImanager.chargeBarScript.SetBarPercent ((c/chargeTimerMax) * 100.0f);
	}

	//AMMO
	void SetRockets (int a)
	{
		if(a < 0)
		{
			Debug.Log ("ERROR: Ammo must be a possitive number");
			Mathf.Clamp(a, 0 , 1000);
		}
		myGUImanager.rocketCounterScript.SetCounterNumber (a);
	}

		//AMMO
	void SetAmmo (int a)
	{
		if(a < 0)
		{
			Debug.Log ("ERROR: Ammo must be a possitive number");
			Mathf.Clamp(a, 0 , 1000);
		}
		myGUImanager.ammoCounterScript.SetCounterNumber (a);
	}
	#endregion
}
